package levelrewind.android.app.adapter;

import java.util.ArrayList;

import levelrewind.android.app.R;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.vo.User;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendsAdapter extends BaseAdapter {

	private ArrayList<User>  friends = new ArrayList<User>();
    private LayoutInflater mInflater;
    private Context context;
    
    public FriendsAdapter(Context context) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }
    
    public void addItem(final User user) {
    	friends.add(user);
        notifyDataSetChanged();
    }
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return friends.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return friends.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return friends.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.friend_score_list, null);
			holder = new ViewHolder();
			holder.name = (TextView)convertView.findViewById(R.id.friend_name);
			holder.score = (TextView)convertView.findViewById(R.id.friend_score);
			holder.image = (ImageView)convertView.findViewById(R.id.friend_image);
			
			holder.name.setTypeface(Util.customFont(context));
			holder.score.setTypeface(Util.customFont(context));
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(friends.get(position).getName());
		String score = Util.formatNumber(friends.get(position).getScore());
		holder.score.setText(score);
		Bitmap pic = friends.get(position).getPicture(); 
		if (pic != null) {
			holder.image.setImageBitmap(pic);
		}

		return convertView;
	}

	public static class ViewHolder {
        public TextView name;
        public TextView score;
        public ImageView image;
    }
}
